import Vue from 'vue'
import Taro from "@tarojs/taro";
import hhhComponent from './components/index'
import {requestData} from "./utils/http";
import './app.less'

Vue.use(hhhComponent)

const App = {
  // 登录
  onShow (options) {
    let user_id = Taro.getStorageSync('user_id')
    if (!user_id) {
      Taro.login({
        success: (res) => {
          requestData("/wxaccess/mp/user/login", {
            "wxlogin_code": res.code
          }).then(res => {
            Taro.removeStorageSync('open_id');
            Taro.removeStorageSync('access_token');
            Taro.removeStorageSync('user_id');
            Taro.removeStorageSync('mobile_binded');
            Taro.removeStorageSync('mobile');
            Taro.setStorageSync('open_id', res?.data?.datas?.open_id || '');
            Taro.setStorageSync('user_id', res?.data?.datas?.user_id || '');
            Taro.setStorageSync('access_token', res?.data?.datas?.access_token ||
              '');
            Taro.setStorageSync('mobile_binded', res?.data?.datas?.mobile_binded ||
              0);
            Taro.setStorageSync('mobile', res?.data?.datas.mobile || '');
          }).catch(err => {
            console.log(err)
          })
        },
        fail: (err) => {
          console.log(err)
        }
      })
    };
  },
  render(h) {
    // this.$slots.default 是将要会渲染的页面
    return h('block', this.$slots.default)
  }
}

export default App
